package com.beaven.imserver.net.service;

import com.beaven.imserver.net.bean.api.account.CategoryModel;
import com.beaven.imserver.net.bean.api.base.ResponseModel;
import com.beaven.imserver.net.bean.card.CategoryCard;
import com.beaven.imserver.net.bean.db.Category;
import com.beaven.imserver.net.factory.CategoryFactory;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * author: Beaven
 * time: 2017/5/23 9:58
 */
@Path("/category")
public class CategoryService extends BaseService {

  // 创建分组
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<CategoryCard> createCategory(CategoryModel model) {
    if (!CategoryModel.check(model)) {
      return ResponseModel.buildParameterError();
    }

    Category category = CategoryFactory.queryByName(getSelf(), model.getName());
    if (category != null) {
      return ResponseModel.buildHaveNameError();
    }

    category = CategoryFactory.create(model.getName(), getSelf());
    if (category != null) {
      CategoryCard categoryCard = new CategoryCard(category);
      return ResponseModel.buildOk(categoryCard);
    } else {
      // 注册异常
      return ResponseModel.buildRegisterError();
    }
  }

  //获取分组列表
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<List<CategoryCard>> getCategories() {
    List<Category> categoryList = CategoryFactory.queryList(getSelf());
    List<CategoryCard> categoryCardList = categoryList.stream()
        .map(CategoryCard::new)
        .collect(Collectors.toList());
    return ResponseModel.buildOk(categoryCardList);
  }
}
